from odoo import api, fields, models, _
import logging
from odoo.exceptions import UserError

logger = logging.getLogger(__name__)


class Feedback_search(models.Model):
    _name = 'feedback.search'
    _description = 'user_info'

    name = fields.Char(
        '反馈表详情',
        default='反馈表详情'
    )
    vaccine_organization = fields.Many2one(
        'vaccine.organization',
        string='接种点',
        required=True
    )
    feedback_no = fields.Char(
        '反馈编号',
        index=True,
        readonly=True,
        default='自动生成'
    )

    department = fields.Selection([
        ('内科', '内科'),
        ('外科', '外科'),
        ('儿科', '儿科'),
        ('妇科', '妇科'),
        ('耳鼻喉科', '耳鼻喉科'),
    ], string='科室', defaut='内科', index=True)
    description = fields.Html(
        '过程描述',
        required=True
    )
    show = fields.Char(
        '表现'
    )
    result = fields.Selection([
        ('痊愈', '痊愈'),
        ('好转', '好转'),
        ('未好转', '未好转'),
        ('不详', '不详'),
        ('有后遗症', '有后遗症'),
        ('死亡', '死亡'),
    ], string='结果', defaut='痊愈', index=True)
    stop_recover = fields.Boolean(
        '停止注射后，反应/事件是否有减弱',
        default=True
    )
    status = fields.Selection([
        ('有效', '有效'),
        ('无效', '无效'),
        ('默认', '默认'),
    ], string='反馈状态', default='默认')

    def define_useful(self):
        self.ensure_one()
        # 检验此数据集中是否包含单条数据，否则报错
        sql_query = """SELECT feedback_no FROM feedback_useful where feedback_no = '{feedback_no}'""".format(
            feedback_no=self.feedback_no)
        self.env.cr.execute(sql_query)
        result_spe = self.env.cr.dictfetchall()
        sql_query = """SELECT feedback_no FROM feedback_useless where feedback_no = '{feedback_no}'""".format(
            feedback_no=self.feedback_no)
        self.env.cr.execute(sql_query)
        result_re = self.env.cr.dictfetchall()
        if result_re:
            raise UserError(_('编号%s的反馈已在无效反馈有记录！') % self.feedback_no)
        if result_spe:
            raise UserError(_('编号%s的反馈已在有效反馈有记录！') % self.feedback_no)
        # 有则报错
        else:
            self.status = '有效'
            return {
                'type': 'ir.actions.act_window',
                'view_type': 'form',
                'view_mode': 'form,tree',
                'res_model': 'feedback.useful',
                'target': 'new',
                'context': {
                    'default_feedback_no': self.feedback_no,
                    'default_department': self.department,
                    'default_description': self.description,
                    'default_show': self.show,
                    'default_result': self.result,
                    'default_stop_recover': self.stop_recover,
                    'default_vaccine_organization': self.vaccine_organization.id,
                }
            }

    def define_useless(self):
        self.ensure_one()
        # 检验此数据集中是否包含单条数据，否则报错
        sql_query = """SELECT feedback_no FROM feedback_useful where feedback_no = '{feedback_no}'""".format(
            feedback_no=self.feedback_no)
        self.env.cr.execute(sql_query)
        result_spe = self.env.cr.dictfetchall()
        sql_query = """SELECT feedback_no FROM feedback_useless where feedback_no = '{feedback_no}'""".format(
            feedback_no=self.feedback_no)
        self.env.cr.execute(sql_query)
        result_re = self.env.cr.dictfetchall()
        if result_re:
            raise UserError(_('编号%s的反馈已在无效反馈有记录！') % self.feedback_no)
        if result_spe:
            raise UserError(_('编号%s的反馈已在有效反馈有记录！') % self.feedback_no)
        # 有则报错
        else:
            self.status = '无效'
            return {
                'type': 'ir.actions.act_window',
                'view_type': 'form',
                'view_mode': 'form,tree',
                'res_model': 'feedback.useless',
                'target': 'new',
                'context': {
                    'default_feedback_no': self.feedback_no,
                    'default_department': self.department,
                    'default_description': self.description,
                    'default_show': self.show,
                    'default_result': self.result,
                    'default_stop_recover': self.stop_recover,
                    'default_vaccine_organization': self.vaccine_organization.id,
                }
            }
